<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    #########################################################################################
    # Programmer : nawattapon yoskamtorn						    #
    # Email	: contact@nawattapon.com						    #
    # Website	: http://www.nawattapon.com						    #
    #########################################################################################
    
    class Admin extends CI_Controller
    {
	function __construct()
	{
	    parent::__construct();
	    if( ! $this->session->userdata('lang')){
                $this->session->set_userdata('lang','english');
            }
            
	    $this->template->set_template('backend');
            $this->lang->load('admin/main', $this->session->userdata('lang'));
	}
	
	public function index()
	{
            if($this->session->userdata('admin_login')){
		redirect('/admin/dashboard/' , 'refresh');
	    }
	    
            $this->login();
	}
        
        public function login()
	{
            if($this->session->userdata('admin_login')){
		redirect('/admin/dashboard/' , 'refresh');
	    }
	    
            if(isset($_GET['lang'])){
                $this->session->set_userdata('lang', $_GET['lang']);
            }
            
	    $data['message'] = $this->session->flashdata('message');
            $this->load->library('form_validation');
	    $this->template->set_template('blank');
	    $this->template->write_view('content','login_view' , $data);
            $this->template->render();
	}
        
        public function auth()
	{
            if($this->session->userdata('admin_login')){
		redirect('/admin/dashboard/' , 'refresh');
	    }
	    
            $this->load->library('form_validation');
            $this->form_validation->set_rules('username', 'lang:username', 'trim|xss_clean|required');
            $this->form_validation->set_rules('password', 'lang:password', 'trim|required');
            
            if ($this->form_validation->run() == FALSE)
            {
                $this->login();
            }
            else
            {
                $this->load->helper('devlism');
                $input['username'] = $this->input->post('username');
                $input['pwd'] = get_encryption($this->input->post('password'));
                
                $this->load->model('user_model');
                
                $this->user_model->set_where($input);
                $user_info = $this->user_model->load_record();
		
		if(is_array($user_info)){
		    $this->session->set_userdata('admin_login',TRUE);
		    $this->session->set_userdata('admin',$user_info);
		    redirect('/admin/dashboard/' , 'refresh');
		}else{
		    $this->session->set_flashdata('message', $this->lang->line('wrong_password'));
		    redirect('/admin/login/' , 'refresh');
		}
            }
	}
	
	public function logout()
	{
	    $this->session->set_userdata('admin_login',FALSE);
	    $this->session->unset_userdata('admin_login');
	    $this->session->set_userdata('admin',array());
	    $this->session->unset_userdata('admin');
            redirect('/admin/login/' , 'refresh');
	}
    }